import { _decorator, Component, director, Label, Node, Sprite } from 'cc';
import { gameConfig } from '../data/gameConfig';
import { loadRes } from '../utils/loadRes';
import { nodePool } from '../utils/nodePool';
import { emits, localData } from '../data/enums';
const { ccclass, property } = _decorator;

@ccclass('winView')
export class winView extends Component {
    @property(Sprite)
    star: Sprite;
    //金币数
    @property(Label)
    jinbiLabel: Label;
    start(): void {
        this.stars();
        this.jinbiUpdate();
    }
    onLevels() {
        nodePool.ins.getPoolNode("levelView", this.node);
    }
    onNext() {
        gameConfig.currentLevel = [];
        gameConfig.currentLevelNum++;
        director.loadScene('game');
    }
    onReplay() {
        gameConfig.currentLevel = [];
        director.loadScene("game");
    }
    stars() {
        if (gameConfig.levelList[gameConfig.currentLevelNum + 1] == -1) {
            gameConfig.levelList[gameConfig.currentLevelNum + 1] = 0;
        }
        if (gameConfig.step > 300) {
            this.star.spriteFrame = loadRes.ins.getSpriteFrame('star1');
            gameConfig.levelList[gameConfig.currentLevelNum] = 1;
        } else if (gameConfig.step > 150) {
            this.star.spriteFrame = loadRes.ins.getSpriteFrame('star2');
            gameConfig.levelList[gameConfig.currentLevelNum] = 2;
        } else {
            this.star.spriteFrame = loadRes.ins.getSpriteFrame('star3');
            gameConfig.levelList[gameConfig.currentLevelNum] = 3;
        }
        const levelListJson = JSON.stringify(gameConfig.levelList);
        localStorage.setItem(localData.playerLevelData, levelListJson);
    }
    jinbiUpdate() {
        //前面加飞金币动画
        gameConfig.jinbiNum += gameConfig.redPoints.length*10;
        localStorage.setItem(localData.jinbiNum, gameConfig.jinbiNum.toString());
        director.emit(emits.jinbi);
    }
}


